<template>
  <div class="menu">
    <template v-if="$route.meta.role === 'admin'">
      <div v-for="(item,index) in navAdminList" :key="index">
        <div class="menu-item" :class="{'selected': item.selected ? true : false}" @click="navPage(item.path)">
          <div class="list-item">
            <div class="space-between">
              <div class="icon">
                <icon-svg :name="item.iconName" class="nav-icon" :class="{'selected': item.selected ? true : false}"/>
              </div>
              <div class="nav-title" :class="{'selected': item.selected ? true : false}">{{item.title}}</div>
            </div>
          </div>
        </div>
      </div>
    </template>
   <template v-else>
     <div v-for="(item,index) in navList" :key="index">
       <div class="menu-item" :class="{'selected': item.selected ? true : false}" @click="navPage(item.path)">
         <div class="list-item">
           <div class="space-between">
             <div class="icon">
               <icon-svg :name="item.iconName" class="nav-icon" :class="{'selected': item.selected ? true : false}"/>
             </div>
             <div class="nav-title" :class="{'selected': item.selected ? true : false}">{{item.title}}</div>
           </div>
         </div>
       </div>
     </div>
   </template>
  </div>
</template>

<script>
  export default {
    name: "Navigation.vue",
    data(){
      return{
        navList: [
          {
            iconName: 'iconperson',
            title: '用户信息',
            path: '/info',
            selected: false
          },
          {
            iconName: 'iconkeben',
            title: '帖子管理',
            path: '/invitation',
            selected: false
          },
          {
            iconName: 'iconguanzhu',
            title: '关注和粉丝',
            path: '/followsFans',
            selected: false
          },
          {
            iconName: 'iconshoucang',
            title: '我的收藏',
            path: '/favorites',
            selected: false
          },
          {
            iconName: 'iconupload',
            title: '资源管理',
            path: '/upload-records',
            selected: false
          },
          {
            iconName: 'iconzhanghaoguanli',
            title: '账号管理',
            path: '/accountManagement',
            selected: false
          },
        ],
        navAdminList: [
          {
            iconName: 'iconperson',
            title: '用户管理',
            path: '/admin-index',
            selected: false
          },
          {
            iconName: 'iconkeben',
            title: '帖子管理',
            path: '/admin-invitation',
            selected: false
          },
          {
            iconName: 'iconbook1',
            title: '资料管理',
            path: '/admin-resourses',
            selected: false
          },
          {
            iconName: 'iconPPTiconcheng',
            title: '课件管理',
            path: '/admin-coursewares',
            selected: false
          },
          {
            iconName: 'iconupload',
            title: '软件管理',
            path: '/admin-softwares',
            selected: false
          }
        ]
      }
    },
    created() {
      this.initNav()
    },
    //监听路由变化
    watch:{
      $route(to,from){
        this.initNav()
      }
    },
    methods: {
      //初始化导航栏跳转显示
      initNav(){
        this.navList.forEach((item) => {
          item.selected = false
        })
        this.navAdminList.forEach((item) => {
          item.selected = false
        })
        if (this.$route.path == '/info' || this.$route.path == '/admin-index'){
          this.navList[0].selected = true
          this.navAdminList[0].selected = true
        }
        else if(this.$route.path == '/invitation' || this.$route.path == '/admin-invitation'){
          this.navList[1].selected = true
          this.navAdminList[1].selected = true
        }
        else if(this.$route.path == '/followsFans' || this.$route.path == '/admin-resourses'){
          this.navList[2].selected = true
          this.navAdminList[2].selected = true
        }
        else if(this.$route.path == '/favorites' || this.$route.path == '/admin-coursewares'){
          this.navList[3].selected = true
          this.navAdminList[3].selected = true
        }
        else if(this.$route.path == '/upload-records' || this.$route.path == '/admin-softwares'){
          this.navList[4].selected = true
          this.navAdminList[4].selected = true
        }
        else if(this.$route.path == '/accountManagement'){
          this.navList[5].selected = true
        }
      },
      // 页面跳转
      navPage(path){
        this.$router.push(path)
        this.initNav()
      },

    }
  }
</script>

<style scoped>
  .menu{
    width: 218px;
    height: 100%;
    background-color: #ffffff;
    border-radius: 4px;
  }
  .nav-icon{
    color: #999999;
    font-size: 12px;
  }
  .menu-item{
    display: flex;
    flex-direction: row;
    justify-content: center;
    height: 58px;
    cursor: pointer;
  }
  .list-item{
    display: flex;
    flex-direction: row;
    justify-content: center;
    width: 170px;
    border-bottom: 1px solid rgba(221,221,221,0.2);
  }
  .space-between{
    display: flex;
    flex-direction: row;
    justify-content: left;
    align-items: center;
    align-content: center;
    width: 130px;
  }
  .nav-title{
    font-family: MicrosoftYaHei;
    font-size: 14px;
    font-weight: normal;
    font-stretch: normal;
    /*line-height: 51px;*/
    letter-spacing: 1px;
    color: #333333;
    margin-left: 12px;
  }
  .menu-item:hover{
    background-color: rgba(27,203,180,0.1);
  }
  .selected{
    background-color: #1bcbb4;
    color: #ffffff;
  }
  .selected:hover{
    background-color: #5dd5c8;
  }
</style>